package cn.yikaojiuguo.student.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.adapter.GoldOrderAdapter;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.GoldOrder;
import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;

public class MyGoldActivity extends CustomerActionBarActivity implements View.OnClickListener{

    @Bind(R.id.tv_money)
    TextView mTv_money;
    @Bind(R.id.tv_charge)
    TextView mTv_charge;
    @Bind(R.id.lv)
    ListView mLv;
    private GoldOrderAdapter mAdapter;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(), MyGoldActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_gold);
        ButterKnife.bind(this);
        initView();
        initEvents();
    }

    private void initView() {
        mTv_money.setText(YiKaoApplication.getGoldCoinNum() + "金币");
    }

    private void initEvents() {
        getData();
        mTv_charge.setOnClickListener(this);
    }

    private void getData() {
        showProgressDialog("正在加载...");
        String requestUrl = UrlConstants.GOLD_ORDER_LIST + "&mid=" + YiKaoApplication.getUserId();
        RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                dismissProgressDialog();
                if (response != null && response.optBoolean("success")) {
                    String goldNum = response.optString("option");
                    if(!goldNum.equals(YiKaoApplication.getGoldCoinNum())) {
                        YiKaoApplication.putGoldCoinNum(goldNum);
                        mTv_money.setText(goldNum + "金币");
                        YiKaoApplication.changeUserInfoCache(true);
                    }
                    Gson gson = new Gson();
                    List<GoldOrder> list = gson.fromJson(response.optJSONArray("data").toString()
                            , new TypeToken<List<GoldOrder>>() {
                    }.getType());
                    if(mAdapter == null) {
                        mAdapter = new GoldOrderAdapter(MyGoldActivity.this,list);
                    } else {
                        mAdapter.addFirstPageData(list);
                    }
                    mLv.setAdapter(mAdapter);
                }
            }
        }, null, mTag);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_charge:
                startActivity(ChargeActivity.newIntent());
                break;
        }
    }

    @Override
    public void onEvent(MessageEvent event) {
        super.onEvent(event);
        if(event.messageType == MessageEvent.MessageType.REFRESH_USER_GOLD_LIST) {
            getData();
        }
    }
}
